<!--  -->
<template>
  <div id="home">
    <nav-bar class="title">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      class="item-control"
      @tabClick="tabClick"
      ref="tabControl1"
      v-show="isfiexdShow"
    ></tab-control>
    <!-- <button v-intervalclick="2000">提交</button> -->
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @backScroll="backScroll"
      :pull-up-load="true"
      @pullingUp="pullingUp"
    >
      <swiper :swiperList="swiperList" @imgLoads="imgLoads"></swiper>
      <recommend :recommend="recommend"></recommend>
      <tab-control
        :titles="['流行', '新款', '精选']"
        class="item-control"
        @tabClick="tabClick"
        ref="tabControl2"
      ></tab-control>
      <good-list :goods="showGoods" ></good-list>
    </scroll>
    <scroll-top @click.native="backTop()" v-show="isShowButton"></scroll-top>
  </div>
</template>

<script>
import navBar from "../../components/common/navBar/navBar";
import swiper from "../../components/content/swiper/swiper";
import goodList from "../../components/content/goods/goodList";
import scroll from "../../components/common/scroll/scroll";
import scrollTop from "../../components/content/scrollTop/scrollTop";

import recommend from "./homeComps/homeRecommend";
import tabControl from "./homeComps/tabControl";
import { homeData, goodsList } from "../../network/home";
import { intervalclick } from "../../intervalclick";
export default {
  components: {
    navBar,
    swiper,
    recommend,
    tabControl,
    goodList,
    scroll,
    scrollTop,
  },
  data() {
    return {
      swiperList: [],
      recommend: [],
      goods: {
        pop: {
          page: 0,
          list: [],
        },
        new: {
          page: 0,
          list: [],
        },
        sell: {
          page: 0,
          list: [],
        },
      },
      currentType: "pop",
      isShowButton: false,
      isfiexdShow: false,
      tanControlOffestTop: 0,
    };
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  watch: {},
  created() {
    // this.$bus.$on("goodimgLoad", () => {
    //   this.$refs.scroll.scroll.refresh();
    // });
    this.getData();
    this.getgoodsList("pop");
    this.getgoodsList("new");
    this.getgoodsList("sell");
  },
  methods: {
    getData() {
      homeData().then((res) => {
        this.swiperList = res.data.banner.list;
        this.recommend = res.data.recommend.list;
      });
    },
    getgoodsList(type) {
      const page = this.goods[type].page + 1;
      goodsList(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        //完成第一次上拉加载更多 后刷新 才可以继续上拉加载数据
        this.$refs.scroll.finishPull();
      });
    },

    //逻辑相关代码

    //选项卡切换对应type加载数据
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    //返回顶部
    backTop() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },
    //返回顶部按钮的显示和隐藏
    backScroll(position) {
      //返回顶部按钮是否展示
      this.isShowButton = -position.y > 1000;
      //计算吸顶效果
      this.isfiexdShow = -position.y > this.tanControlOffestTop;
    },
    //上拉加载更多
    pullingUp() {
      this.getgoodsList(this.currentType);
    },

    //tabControl 粘性布局 获取距离顶部的高度
    imgLoads() {
      this.tanControlOffestTop = this.$refs.tabControl2.$el.offsetTop;
    },
   
  },
  mounted() {},
  updated() {},
  // 滥情男女 情仇爱恨别离 一代人终将老去 但总有人正年轻！~
};
</script>
<style lang="less" scoped>
.home {
  height: 100vh;
}
.title {
  background: palevioletred;
}
.item-control {
  position: relative;
  z-index: 9;
  top: 0;
  background: #ffffff;
}
.content {
  overflow: hidden;
  position: absolute;
  top: 40px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>
